<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models\Module;

use App\Models\Api\Apis;
use App\Models\Projects\Product;
use App\Models\Projects\Project;
use App\Models\Projects\ProjectCate;
use App\Models\TestApi\TestModule;
use Carbon\Carbon;
use App\Models\Eloquent;
use App\Models\Meimei\Traits\SignOssUrlTrait;
use App\Models\Meimei\Traits\ExplodeTrait;

/**
 * Class QtpProductModule
 *
 * @property int $id
 * @property int $cate_id
 * @property int $project_id
 * @property int $product_id
 * @property int $p_id
 * @property int $level
 * @property string $name
 * @property string $remark
 * @property string $note
 * @property string $tags
 * @property string $created_adminer
 * @property string $updated_adminer
 * @property int $sort
 * @property int $enable
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 *
 * @package App\Models
 */
class ProductModule extends Eloquent
{

    use ExplodeTrait;
    use SignOssUrlTrait;
    protected $explode = ['tags'];

	protected $table = 'product_modules';

	protected $casts = [
		'cate_id' => 'int',
		'project_id' => 'int',
		'product_id' => 'int',
		'p_id' => 'int',
		'level' => 'int',
		'sort' => 'int',
		'enable' => 'int'
	];

	protected $fillable = [
		'cate_id',
		'project_id',
		'product_id',
		'p_id',
		'level',
		'name',
		'remark',
		'note',
		'tags',
		'created_adminer',
		'updated_adminer',
		'sort',
		'enable'
	];

    public function label()
    {
        $this->cate_name = ProjectCate::find($this->cate_id)->name ?? '';
        ($this->p_id > 0) && $this->p_name = static::find($this->p_id)->name ?? '';
        ($this->p_id > 0) && $this->api_count = Apis::where('s_module_id', $this->id)->count();
        ($this->p_id == 0) && $this->api_count = Apis::where('f_module_id', $this->id)->count();
        ($this->p_id == 0) && $this->s_module_sum = self::where('p_id', $this->id)->count();
        $this->project_name = Project::find($this->project_id)->name ?? '';
        $this->product_name = Product::find($this->product_id)->name ?? '';
        ($this->p_id > 0) && $this->test_sum = TestModule::where('s_module_id', $this->id)->count();
        ($this->p_id == 0) && $this->test_sum = TestModule::where('f_module_name', $this->name)->count();
        return $this;
    }

    public static $fillables = [
        'cate_id' => '分类ID',
        'project_id' => '项目ID',
        'product_id' => '所属产品ID',
        'p_id' => '父级ID',
        'level' => '层级',
        'name' => '模块名称',
        'remark' => '模块说明',
        'note' => '模块备注',
        'tags' => '模块标签',
        'created_adminer' => '创建人',
        'updated_adminer' => '更新人',
        'sort' => '排序',
        'enable' => '状态'
    ];

    public static function getFidsByProductId(int $product_id): ?array
    {
        return self::where('product_id', $product_id)
            ->select(['id', 'sort'])
            ->where('enable', 1)
            ->where('level', 1)
            ->where('p_id', 0)
            ->orderByDesc('sort')
            ->pluck('id')?->toArray();
    }

    public static function getFidsByFIds2(array $f_module_ids): ?array
    {
        return self::whereIn('id', $f_module_ids)
            ->select(['id', 'sort'])
            ->where('enable', 1)
            ->where('level', 1)
            ->where('p_id', 0)
            ->orderByDesc('sort')
            ->pluck('id')?->toArray();
    }

    public static function getFidsBySIds(array $s_module_ids): ?array
    {
        $p_ids = self::select('id', 'p_id')->whereIn('id', $s_module_ids)->pluck('p_id')?->toArray();
        if(empty($p_ids)) {
            return [];
        }
        return self::whereIn('id', $p_ids)
            ->select(['id', 'sort'])
            ->where('enable', 1)
            ->where('level', 1)
            ->where('p_id', 0)
            ->orderByDesc('sort')
            ->pluck('id')?->toArray();
    }

    public static function getFidsByFids(array $fids): ?array
    {
        $sids = [];
        foreach($fids as $fid) {
            $sidsf = self::where('p_id', $fid)
                ->select(['id', 'sort'])
                ->where('enable', 1)
                ->where('level', 2)
                ->where('p_id', '>', 0)
                ->orderByDesc('sort')
                ->pluck('id')?->toArray();
            if(empty($sidsf)) {
                continue;
            }
            $sids[] = $sidsf;
        }
        if(empty($sids)) {
            return [];
        }
        $fun = function ($a) {
            $c = [];
            foreach ($a as $b) {
                foreach ($b as $d) {
                    $c[] = $d;
                }
            }
            return $c;
        };
        return $fun($sids);
    }

    public static function getSidsBySids(array $Sids): ?array
    {
        $sids = [];
        foreach($Sids as $fid) {
            $sidsf = self::where('id', $fid)
                ->select(['id', 'sort'])
                ->where('enable', 1)
                ->where('level', 2)
                ->where('p_id', '>', 0)
                ->orderByDesc('sort')
                ->pluck('id')?->toArray();
            if(empty($sidsf)) {
                continue;
            }
            $sids[] = $sidsf;
        }
        if(empty($sids)) {
            return [];
        }
        $fun = function ($a) {
            $c = [];
            foreach ($a as $b) {
                foreach ($b as $d) {
                    $c[] = $d;
                }
            }
            return $c;
        };
        return $fun($sids);
    }
}
